﻿using System.Collections.Generic;
using System.Windows.Forms;

namespace ThirdLife.UI.WinForms
{
  public partial class MainScreen : Form, IMainScreenView
  {
    private IMetaverseModelAdapter _adapter;
    private Dictionary<int, IDockableUserControlView> _dockedControls;
    private Dictionary<int, IDockLocation> _orderedControlLocations;
    private SortedList<int, IDockableUserControlView> _orderedControls;
    private MainScreenUIModule _module;

    public MainScreen(MainScreenUIModule module)
    {
      _module = module;
      InitializeComponent();
      Text = ThirdLifeResources.Instance.GetText("_ThirdLife");
      Icon = ThirdLifeResources.Instance.GetIcon(("ThirdLifeIcon"));
    }

    #region IMainScreenView Members

    public void SetupView()
    {
      _dockedControls = new Dictionary<int, IDockableUserControlView>();
      _orderedControls = new SortedList<int, IDockableUserControlView>();
      _orderedControlLocations = new Dictionary<int, IDockLocation>();
    }

    public void DestroyView()
    {
      HideWindow();
    }

    public void AddDockableControl(IDockableUserControlView control, object location)
    {
      _dockedControls.Add(control.ControlId, control);
      IDockLocation loc = location as IDockLocation;
      _orderedControls.Add(loc.Order, control);
      _orderedControlLocations.Add(control.ControlId, loc);
      RelayoutControls();
    }

    public void SetModelAdapter(IMetaverseModelAdapter adapter)
    {
      _adapter = adapter;
    }

    public object Control
    {
      get { return this; }
    }

    public void ShowWindow()
    {
      Show();
    }

    public void HideWindow()
    {
      Hide();
    }

    #endregion

    private void RelayoutControls()
    {
      //lets try to calculate the total height sum of controls that are not-fixed, and thus will use relative proportions
      float heightSum = 0;
      foreach (KeyValuePair<int, IDockableUserControlView> pair in _orderedControls)
      {
        if (_orderedControlLocations[pair.Value.ControlId].Fixed == false)
        {
          int h = _orderedControlLocations[pair.Value.ControlId].Height;
          heightSum += h;
        }
      }

      //clear out all our styles, and make only a single column of rows
      tableLayoutPanel1.SuspendLayout();
      tableLayoutPanel1.Controls.Clear();
      tableLayoutPanel1.ColumnCount = 1;
      tableLayoutPanel1.RowCount = _orderedControls.Count;
      tableLayoutPanel1.ColumnStyles.Clear();
      tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
      tableLayoutPanel1.RowStyles.Clear();

      int row = 0;
      //go through all our controls in their order
      foreach (KeyValuePair<int, IDockableUserControlView> pair in _orderedControls)
      {
        //get the height
        int h = _orderedControlLocations[pair.Value.ControlId].Height;
        //get the controls
        Control c = pair.Value.Control as Control;
        //if they are not fixed, calculate a percentage relative to the control size
        if (_orderedControlLocations[pair.Value.ControlId].Fixed == false)
        {
          float hRatio = 100*h/heightSum;
          c.Dock = DockStyle.Fill;
          tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent,
                                                       hRatio));
        }
          //otherwise, just give it a fixed size
        else
        {
          c.Dock = DockStyle.Fill;
          tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute,
                                                       h));
        }
        tableLayoutPanel1.Controls.Add(c, 0, row);
        row++;
      }
      tableLayoutPanel1.ResumeLayout();
    }

    private void MainScreen_FormClosing(object sender, FormClosingEventArgs e)
    {
      _module.WindowIsClosing();
    }
  }
}